package com.smalljj.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smalljj.bean.User;
import com.smalljj.bean.UserKey;
import com.smalljj.service.UserService;
import com.smalljj.util.SmalljjUtil;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 注册
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/register",method=RequestMethod.POST )
    @ResponseBody
	public Map<String,Object> register(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String userName  = request.getParameter("username");
		String password  = request.getParameter("password");
		if( userName==null || userName.equals("")){//空提交
			jsonMap.put("result", false);
			jsonMap.put("message", "注册失败,原因:用户名为空!");
			return jsonMap;
		}
		User existUser = userService.findUserByName(userName);
		if(existUser!=null){//用户存在
			jsonMap.put("result", false);
			jsonMap.put("message", "注册失败,原因:用户已存在!");
			return jsonMap;
		}
		User newUser = new User();
		newUser.setUsername(userName);
		newUser.setPassword(password);
		newUser.setRegdate(new BigDecimal(SmalljjUtil.getCurrentFormat("yyyyMMdd")));
		boolean result  = userService.insert( newUser );
		if(result){
			newUser = userService.findUserByName(userName);
			jsonMap.put("result", true);
			jsonMap.put("userid", newUser.getId());
			jsonMap.put("regdate", newUser.getRegdate() );
			return jsonMap;
		}else{
			jsonMap.put("result", false);
			jsonMap.put("message", "注册失败!");
			return jsonMap;
		}
	}

	/**
	 * 登录
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/login",method=RequestMethod.POST )
    @ResponseBody
	public Map<String,Object> login(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String userName = request.getParameter("username").replaceAll(".*([';]+|(--)+).*", "");//防sql注入
		String password = request.getParameter("password").replaceAll(".*([';]+|(--)+).*", "");//防sql注入
		User user = userService.findUserByName( userName );
		if(user==null || user.getPassword().equals(password)==false){
			jsonMap.put("result", false);
			jsonMap.put("message", "登录失败!请检查用户名与密码!");
			return jsonMap;
		}else{
			user.setNowlat("");//TODO
			user.setNowlon("");//TODO
			user.setLastActiveDate(new BigDecimal(SmalljjUtil.getCurrentFormat("yyyyMMdd")));
			user.setLastActiveTime(SmalljjUtil.getCurrentFormatStr("HH:mm"));
			user.setStaytime(new BigDecimal(1));
			userService.updateUser(user);
			
			jsonMap.put("result", true);
			jsonMap.put("userid", user.getId());
			jsonMap.put("regdate", user.getRegdate() );

			return jsonMap;
		}
	}
	/**
	 * 找回密码第一步
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/getPasswordStep1",method=RequestMethod.POST )
    @ResponseBody
	public Map<String,Object> getPasswordStep1(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String userName = request.getParameter("username");
		User existUser = userService.findUserByName(userName);
		if(existUser==null){//用户不存在
			jsonMap.put("result", false);
			jsonMap.put("message", "用户不存在!");
			return jsonMap;
		}else{
			if(existUser.getQuestion()==null||existUser.getQuestion().trim().equals("")){//用户未设置问题
				jsonMap.put("result", false);
				jsonMap.put("message", "用户未设置问题!");
				return jsonMap;
			}else{
				jsonMap.put("result", true);
				jsonMap.put("userid", existUser.getId());
				jsonMap.put("question", existUser.getQuestion());
				return jsonMap;
			}
		}
	}
	
	/**
	 * 找回密码第二步
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/getPasswordStep2",method=RequestMethod.POST )
    @ResponseBody
	public Map<String,Object> getPasswordStep2(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String userid = request.getParameter("userid");
		String answer = request.getParameter("answer");
		User user = userService.findByKey( new UserKey( new BigDecimal(userid) ) );
		if(user==null || user.getAnswer()==null || user.getAnswer().equalsIgnoreCase(answer)==false){
			jsonMap.put("result", false);
			jsonMap.put("message", "问题答案不正确!");
			return jsonMap;
		}else{
			jsonMap.put("result", true);
			jsonMap.put("message", "您的密码是:"+user.getPassword());
			return jsonMap;
		}
	}
}